﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 三国题库
{
    public partial class RegionSelectorForm : Form
    {
        private Point startPoint;
        private Rectangle selectedRegion;
        private bool isSelecting = false;

        public Rectangle SelectedRegion => selectedRegion;

        public RegionSelectorForm()
        {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            this.WindowState = FormWindowState.Maximized;
            this.TopMost = true;
            this.DoubleBuffered = true;
            this.Opacity = 0.5; // 半透明背景
        }

        // 鼠标按下时记录起始点
        private void RegionSelectorForm_MouseDown(object sender, MouseEventArgs e)
        {
            startPoint = e.Location;
            isSelecting = true;
        }

        // 鼠标移动时绘制选区
        private void RegionSelectorForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (!isSelecting) return;

            int x = Math.Min(startPoint.X, e.X);
            int y = Math.Min(startPoint.Y, e.Y);
            int width = Math.Abs(e.X - startPoint.X);
            int height = Math.Abs(e.Y - startPoint.Y);

            selectedRegion = new Rectangle(x, y, width, height);
            this.Invalidate(); // 触发重绘
        }

        // 鼠标释放时关闭窗体
        private void RegionSelectorForm_MouseUp(object sender, MouseEventArgs e)
        {
            isSelecting = false;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        // 绘制选区矩形
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            if (selectedRegion != Rectangle.Empty)
            {
                using (Pen pen = new Pen(Color.Red, 2))
                {
                    e.Graphics.DrawRectangle(pen, selectedRegion);
                }
            }
        }
    }
}
